/*******************************************
 * Copyright 2007 Aphrodite Framework for javaME .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * Email : owner@aphroditeme.dev.java.net
 * Contributors:
 *     Kent Elton - initial API and implementation
 *******************************************/

package com.aphrodite.ui.widgets;

import javax.microedition.lcdui.Graphics;

import com.aphrodite.util.ArrayUtil;

//TBD scrolling for the menu, must implement traverse
public final class PopUpMenu extends TraversableWidget {

	private boolean animationComplete;
	private int maxVisible;
	private String[] menuCaption;

	private int selectedIndex = 0;

	private int theight;

	private int twidth;

	/**
	 * @param theme
	 * @param menu
	 *            menu caption
	 * @param maxVisible
	 *            max number of menu's visible. a scroll is performed if the
	 *            number of menu's is exceeded
	 */
	public PopUpMenu(Theme theme, String[] menu, int maxVisible) {
		super(theme, 0, 0);
		this.maxVisible = maxVisible;
		this.menuCaption = menu;
		updateModel();
	}

	public void cleanUp() {
		menuCaption = null;

	}

	private int getLongestMenu(String[] menu) {
		int[] stringw = new int[menu.length];
		for (int i = 0; i < stringw.length; i++) {
			stringw[i] = theme.widgetFont.stringWidth(menu[i]);
		}
		return ArrayUtil.getBiggestValue(stringw, 0, stringw.length);
	}

	public void hide() {
		isVisible = false;
		reDrawWidget();
	}

	public boolean moveDown() {
		if (selectedIndex < menuCaption.length - 1) {
			selectedIndex++;
			reDrawWidget();
			return true;
		} else
			return false;

	}

	public boolean moveUp() {
		if (selectedIndex > 0) {
			selectedIndex--;
			reDrawWidget();
			return true;
		} else
			return false;
	}

	public void render(Graphics g) {
		if (!isVisible)
			return;
		capture(g, twidth, theight, true);
		if (animationComplete) {
			// g.setColor(borderColor);
			// g.drawRect(x, y, width, height);
			g.setFont(theme.widgetFont);
			int pad = 2 + theme.widgetFont.getHeight();
			int shade = com.aphrodite.image.ColorUtil.setLuminosity(theme.bgColor, 50);
			for (int i = 0; i < menuCaption.length; i++) {
				if (i == selectedIndex) {
					g.setColor(0x0000ff);
					g.fillRect(2, (i * pad) + 2, width - 4, (theme.widgetFont).getHeight());
				}
				g.setColor(theme.fgColor);
				g.drawString(menuCaption[i], 2, (i * pad) + 2, 0);
				g.setColor(0x000000);
				g.drawLine(2, (i * pad), width - 4, (i * pad));
				g.setColor(shade);// 0x414141);
				g.drawLine(2, (i * pad) + 1, width - 4, (i * pad) + 1);
			}
		}
		release(g, twidth, theight, true);

	}

	// public void setFont(Font font) {
	// super.setFont(font);
	// updateModel();
	// }

	public void setMenu(String[] menu) {
		this.menuCaption = menu;
		updateModel();
	}

	public void show() {
		isVisible = true;
		selectedIndex = 0;
		startAnimation();
		// reDrawWidget();
	}

	private void startAnimation() {
		animationComplete = false;
		twidth = theight = 0;
		int Wincr = (width % 2 == 0) ? 2 : 1;
		int Hincr = (height % 2 == 0) ? 2 : 1;
		new Thread(new Runnable() {
			public void run() {
				for (int i = 0; i < width; i++) {
					twidth++;
					theight++;
					try {
						Thread.sleep(10);
						reDrawWidget();
					} catch (InterruptedException e) {
					}
				}
				animationComplete = true;
				reDrawWidget();
			}
		}).start();
	}

	protected boolean traverse(int dir, int viewportWidth, int viewportHeight, int[] visRect_inout) {
		// TODO Auto-generated method stub
		return false;
	}

	protected void updateModel() {
		width = 2 + getLongestMenu(menuCaption) + 2;
		height = 2 + (2 * menuCaption.length - 1) + (menuCaption.length * theme.widgetFont.getHeight());
	}

}
